using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Services.FileSystem;
using Deepcode.Flux.Core.Systems.Asset;
using Deepcode.Flux.Core.Extensions;

public partial class Private_Asset_Actions_NewFolder : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxAmAccess"; } }
	#endregion
	#region ViewState and Properties
	/// <summary>
	/// Store the path passed in if appropriate
	/// </summary>
	protected string Path
	{
		get
		{
			object o = ViewState["Path"];
			if( o == null ) return "";
			return (string) o;
		}
		set
		{
			ViewState["Path"] = value;
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			// Load path data
			Path = GetAttribute("Parent", "");
			// Display the path in the panel
			breadcrumbs.Text = Path.Substring(1);
		}
	}
	#endregion
	#region Navigation
	/// <summary>
	/// Go back to the previous form
	/// </summary>
	/// <param name="refreshFolders"></param>
	/// <param name="refreshFiles"></param>
	protected void GoBack(bool refreshFolders, bool refreshFiles)
	{
		string url = "~/admin/private/asset/actions.aspx?path=" + this.Path;
		if( refreshFolders ) url += "&refreshFolders=true";
		if( refreshFiles ) url += "&refreshFiles=true";

		Response.Redirect(url, true);
	}
	#endregion
	#region Button handlers
	/// <summary>
	/// Cancel
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancelClicked(object sender, EventArgs e)
	{
		GoBack( false, false );
	}
	/// <summary>
	/// 
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCreateClicked(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;

		string fp = this.Path;
		if( ! fp.EndsWith("/") ) fp += "/";
		fp += foldername.Text;

		try
		{
			string fullpath = Server.MapPath(fp);
			System.IO.Directory.CreateDirectory(fullpath);

			Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseImportant(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
				"Flux", "Asset Manager",
				"created asset folder {0}", fullpath);

		}
		catch { }
		GoBack(true, true);
	}
	#endregion
}
